package com.grape.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.grape.action.common.BaseAction;
import com.grape.domain.base.Area;
import com.grape.service.base.AreaService;
import com.grape.utils.PinYin4jUtils;

@Controller
@Namespace("/area")
@Scope("prototype")
@ParentPackage("json-default")
public class AreaAction extends BaseAction<Area>{
	
	// 条件分页
	@Action(value="findall",results=@Result(name="success",type="json"))
	public String findAll() {
		
		// 基本分页
		Pageable pageable = new PageRequest(page-1, rows);
		
		
		// 条件查询
		Specification<Area> sa = new Specification<Area>() {
			
			List<Predicate> lp = new ArrayList<>();
			
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				
				// 条件一,查询省
				if (StringUtils.isNotBlank(model.getProvince())) {
					Predicate p1 = cb.like(root.get("province").as(String.class), "%"+model.getProvince()+"%");
					lp.add(p1);
				}
				
				// 条件二,查询市
				if (StringUtils.isNotBlank(model.getCity())) {
					Predicate p2 = cb.like(root.get("city").as(String.class), "%"+model.getCity()+"%");
					lp.add(p2);
				}
				
				// 条件三,查询区
				if (StringUtils.isNotBlank(model.getDistrict())) {
					Predicate p3 = cb.like(root.get("district").as(String.class), "%"+model.getDistrict()+"%");
					lp.add(p3);
				}
				
				return cb.and(lp.toArray(new Predicate[0]));
			}
		};

		Page<Area> page = as.findAll(sa,pageable);
		pushPageDataToValueStack(page);
		
		return "success";
	}
	
	
	
	
	
	
	
	

	@Action(value="batchImport")
	public void batchImport() throws Exception {
		// 获取这个文件使用.xls解析 数据入库
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
		HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
		
		// 保存处理好的每个地区
		List<Area> la = new ArrayList<>();
		
		for (Row row : sheetAt) {
			// 跳过表头
			if (row.getRowNum()==0) {
				continue;
			}
			
			
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			
			// 省，市，区，邮编
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			
			String province = area.getProvince();
			String city = area.getCity();
			String district = area.getDistrict();
			
			province = province.substring(0,province.length()-1);
			city = city.substring(0, city.length()-1);
			district = district.substring(0,district.length()-1);
			String m = province+city+district;
			
			// 用户可以通过拼音码直接检索数据
			
			// 简码
			String[] headByString = PinYin4jUtils.getHeadByString(m);
			StringBuffer sb = new StringBuffer();
			for (String str : headByString) {
				sb.append(str);
			}
			
			area.setShortcode(sb.toString());
			
			// 全码
			String hanziToPinyin = PinYin4jUtils.hanziToPinyin(m,"");
			area.setCitycode(hanziToPinyin);
			
			la.add(area);
			
		}
		
		as.saveBatch(la);
		
	}
	
	
	
	@Autowired
	private AreaService as;
	
	private File file;
	public void setFile(File file) {
		this.file = file;
	}
	
}
